package com.jdcjk.project.account.view;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jdcjk.R;
import com.jdcjk.UrlAddress;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.base.view.BaseFragment;
import com.jdcjk.project.account.model.entity.Result;
import com.jdcjk.project.account.presenter.RegisteredPresenter;
import com.jdcjk.project.login.view.LoginActivity;

import okhttp3.Call;
import okhttp3.Request;

import static com.jdcjk.R.id.back_btn;

/**
 * Created by Administrator on 2016/12/14 0014.
 */

public class ResetPwdFragment extends BaseFragment<RegisteredPresenter> {
    private final String TAG = RegisteredFragment.class.getSimpleName();
    private ImageButton back_button;
    private TextView barTitle_textview;
    //手机号码 EditText
    private EditText loginPhone_editText;
    //验证码EditText
    private EditText verificationCode_editText;
    //获得验证码TextView
    private TextView verificationCode_textView;
    //重置密码EditText
    private EditText resetPassword_editText;
    //确认重置密码EditText
    private EditText confirmResetPassword_et;
    //重置密码Button
    private Button retrievePwd_btn;

    @Override
    public RegisteredPresenter bindPresenter() {
        presenter = new RegisteredPresenter(getContext());
        return presenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_retrieve_pwd;
    }

    @Override
    public void initContentView(View viewContent) {
        back_button = (ImageButton) viewContent.findViewById(back_btn);
        back_button.setOnClickListener(this);
        barTitle_textview = (TextView) viewContent.findViewById(R.id.bar_title_textview);
        barTitle_textview.setText(R.string.retrieve_pwd);
        loginPhone_editText = (EditText) viewContent.findViewById(R.id.login_phone_et);
        resetPassword_editText = (EditText) viewContent.findViewById(R.id.reset_password_et);
        confirmResetPassword_et = (EditText) viewContent.findViewById(R.id.confirm_reset_password_et);
        verificationCode_editText = (EditText) viewContent.findViewById(R.id.verification_code_et);
        verificationCode_textView = (TextView) viewContent.findViewById(R.id.retpwd_verification_code_tv);
        verificationCode_textView.setOnClickListener(this);
        retrievePwd_btn = (Button) viewContent.findViewById(R.id.fragment_retrieve_pwd_btn);
        retrievePwd_btn.setOnClickListener(this);
    }

    public void retrievePwd() {
        String loginNumber = loginPhone_editText.getText().toString();
        String verificationCode = verificationCode_editText.getText().toString();
        String resetPassword = resetPassword_editText.getText().toString();
        String confirmPassword = confirmResetPassword_et.getText().toString();
        if (loginNumber.length() == 0 || verificationCode.length() == 0 || resetPassword.length() == 0 || confirmPassword.length() == 0) {
            Toast.makeText(getActivity(), R.string.toast_lack_info, Toast.LENGTH_SHORT).show();
            return;
        }
        if (resetPassword.length() < 6) {
            Toast.makeText(getActivity(), R.string.toast_password_is_less_than_six, Toast.LENGTH_SHORT).show();
            return;
        }
        if (!resetPassword.equals(confirmPassword)) {
            Toast.makeText(getActivity(), R.string.toast_confirm_password_error, Toast.LENGTH_SHORT).show();
            return;
        }
        if (verificationCode.length() < 6) {
            Toast.makeText(getActivity(), R.string.toast_verification_code_error, Toast.LENGTH_SHORT).show();
            return;
        }
        if (!loginNumber.equals("") && !resetPassword.equals("") && !verificationCode.equals("")) {
            presenter.resetPwd(UrlAddress.UPDATE_PASSWORD, loginNumber, verificationCode, resetPassword, new BasePresenter.OnUIThreadListener<Result>() {
                @Override
                public void onBefore(Request request, int id) {

                }

                @Override
                public void onAfter(int id) {

                }

                @Override
                public void onError(Call call, Exception e, int i) {
                    Toast.makeText(getActivity(), R.string.toast_updata_fail, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(Result result) {
                    switch (result.getStatus()) {
                        case "OK":
                            Toast.makeText(getActivity(), R.string.toast_updata_success, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                            break;
                        case "FAILED":
                            Toast.makeText(getActivity(), R.string.toast_updata_fail, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    /**
     * 获得验证码
     */
    public void getVerificationCode() {
        String phone = loginPhone_editText.getText().toString();
        if (phone.equals("") || phone.trim().length() != 11) {
            Toast.makeText(getActivity(), "请填写正确的11位手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取验证码
        presenter.getVerificationCode(UrlAddress.GET_CODE, phone, "SMS_7395037", new BasePresenter.OnUIThreadListener<Result>() {
            @Override
            public void onBefore(Request request, int id) {

            }

            @Override
            public void onAfter(int id) {

            }

            @Override
            public void onError(Call call, Exception e, int i) {
                Message msg = new Message();
                msg.what = 7;
                handler.sendMessage(msg);
            }

            @Override
            public void onResponse(Result result) {
                Message msg;
                switch (result.getStatus()) {
                    case "OK":
                        msg = new Message();
                        msg.what = 1;
                        verificationCode_textView.setClickable(false);
                        handler.sendMessage(msg);
                        break;
                    case "FAILED":
                        msg = new Message();
                        msg.what = 14;
                        handler.sendMessage(msg);
                    default:
                        msg = new Message();
                        msg.obj = result.getMessage();
                        msg.what = 7;
                        handler.sendMessage(msg);
                        break;
                }
            }
        });
    }

    //验证码返回值判断handler
    private int number = 60;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (number == 0) {
                        verificationCode_textView.setTextSize((float) 15);
                        verificationCode_textView.setText("获取验证码");
                        verificationCode_textView.setClickable(true);
                        number = 60;
                    } else {
                        verificationCode_textView.setTextSize((float) 20);
                        verificationCode_textView.setText(number + "s");
                        number--;
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Message msg = new Message();
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }
                        }, 1000);
                    }
                    break;
                case 14:
                    Toast.makeText(getActivity(), "发送失败", Toast.LENGTH_SHORT).show();
                    break;
                case 7:
                    Toast.makeText(getActivity(), R.string.system_exception, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
